
package net.atremmedia.gwt.actionjava.examples.bitmap.samples;

import net.atremmedia.gwt.actionjava.examples.AJExample;
import net.atremmedia.gwt.actionjava.examples.frame.AJExampleOverview;
import actionjava.display.Shape;
import actionjava.display.Sprite;
import actionjava.events.tick.TickEvent;
import actionjava.geom.Color;
import com.google.gwt.user.client.Timer;

public class BitmapExample1 extends AJExample
{
	public static final String NAME = "BitmapExample 1";
	public static final String DESCRIPTION = "Description";
	public static final String EXAMPLE_SRC = "https://code.google.com/p/gwt-actionjava/source/browse/trunk/aj-examples/aj-examples-bitmap/src/main/java/net/atremmedia/gwt/actionjava/examples/bitmap/samples/BitmapExample1.java";

	private Timer redrawTimer;
	private Sprite rotationContainer;
	private Shape shape;

	public BitmapExample1(AJExampleOverview base)
	{
		super(NAME, DESCRIPTION, EXAMPLE_SRC, base);
	}

	@Override
	public void run()
	{
		rotationContainer = new Sprite();
		rotationContainer.setX(250);
		rotationContainer.setY(250);

		shape = new Shape();
		drawCircles();

		rotationContainer.addChild(shape);
		addChild(rotationContainer);

		shape.setCacheAsBitmap(true);

		initTimer();
	}

	private void drawCircles()
	{
		double numCircles = getRandomNumber(1, 10);
		int color;
		double x, y, radius;
		for(int i = 0;i < numCircles;i++) {
			x = getRandomNumber(-100, 100);
			y = getRandomNumber(-100, 100);
			radius = getRandomNumber(15, 50);
			color = (int) getRandomNumber(0, 16581375);
			shape.getGraphics().beginFill(Color.getRGB(color));
			shape.getGraphics().moveTo(x, y);
			shape.getGraphics().drawRect(x, y, radius, radius);
			shape.getGraphics().endFill();
		}
	}

	private void initTimer()
	{
		redrawTimer = new Timer()
		{
			@Override
			public void run()
			{
				shape.getGraphics().clear();
				drawCircles();
			}
		};
		redrawTimer.scheduleRepeating(500);
	}

	@Override
	public void destroy()
	{
		super.destroy();
		redrawTimer.cancel();
	}

	@Override
	public void onRenderTick(TickEvent event)
	{
		shape.setRotation(shape.getRotation() + 3);

		// drawBounds.setRotation(rotationContainer.getRotation() + 1);

		/*
		 * IRectangle2D bounds = rotationContainer.getTransformedBounds(); if(bounds != null) { // Log.info("x: " + bounds.getX() + " y: " + bounds.getY() + " width: " + bounds.getWidth() +
		 * " height: " + bounds.getHeight()); IGraphics graph = drawBounds.getGraphics(); graph.clear(); graph.beginStroke(Color.getRGB(00, 00, 00)); graph.drawRect(bounds.getX(), bounds.getY(),
		 * bounds.getWidth(), bounds.getHeight()); graph.endStroke(); }
		 */
	}
}
